// +----------------------------------------------------------------------
// | CmsWing [ 网站内容管理框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2115 http://www.cmswing.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: arterli <arterli@qq.com>
// +----------------------------------------------------------------------
const crypto = require('crypto');
const fs = require('fs');
module.exports = class extends think.cmswing.center {
  /**
   * index action
   * @return {Promise} []
   */
  async indexAction() {
    // auto render template file index_index.html
    if (this.is_login > 0) {
      if (this.isPost) {
        const userId = this.is_login;
        const {orderId, persent, fukuanjine} = this.post();
        const order = await this.model('order').where({id: orderId, status: 3, xuid: userId}).find();
        if (!think.isEmpty(order)) {
          const fukuanno = await this.model('cmswing/order').orderid(userId);
          const data = {
            order_no: order.order_no,
            fukuanno: fukuanno,
            user_id: userId,
            user_gsmc: this.user.gsmc,
            shoukuan_id: order.gongid,
            status: 0,
            chuangjianshijian: new Date().valueOf(),
            persent: persent,
            fukuanjine: fukuanjine,
            htbh: order.htbh
          };
          const insertId = await this.model('fukuandan').add(data);
          if (!think.isEmpty(insertId)) {
            return this.success({name: '付款成功！'});
          } else {
            return this.fail('付款失败');
          }
        } else {
          return this.fail('付款失败');
        }
      } else {
        const id = this.get('id');
        const order = await this.model('order').find(id);
        this.assign('order', order);
        return this.display();
      }
    } else {
      return this.fail('付款失败');
    }
  }
};
